package cn.iocoder.yudao.module.train.service.studentcourseperformance;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.StudentCoursePerformanceStudentDTO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.train.controller.admin.studentcourseperformance.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.studentcourseperformance.StudentCoursePerformanceDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.train.dal.mysql.studentcourseperformance.StudentCoursePerformanceMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.train.enums.ErrorCodeConstants.*;

/**
 * 学生课程作品 Service 实现类
 *
 * @author waynelam
 */
@Service
@Validated
public class StudentCoursePerformanceServiceImpl implements StudentCoursePerformanceService {

    @Resource
    private StudentCoursePerformanceMapper studentCoursePerformanceMapper;

    @Override
    public Integer createStudentCoursePerformance(StudentCoursePerformanceSaveReqVO createReqVO) {
        // 插入
        StudentCoursePerformanceDO studentCoursePerformance = BeanUtils.toBean(createReqVO, StudentCoursePerformanceDO.class);
        studentCoursePerformanceMapper.insert(studentCoursePerformance);

        // 返回
        return studentCoursePerformance.getId();
    }

    @Override
    public void updateStudentCoursePerformance(StudentCoursePerformanceSaveReqVO updateReqVO) {
        // 校验存在
        validateStudentCoursePerformanceExists(updateReqVO.getId());
        // 更新
        StudentCoursePerformanceDO updateObj = BeanUtils.toBean(updateReqVO, StudentCoursePerformanceDO.class);
        studentCoursePerformanceMapper.updateById(updateObj);
    }

    @Override
    public void deleteStudentCoursePerformance(Integer id) {
        // 校验存在
        validateStudentCoursePerformanceExists(id);
        // 删除
        studentCoursePerformanceMapper.deleteById(id);
    }

    @Override
        public void deleteStudentCoursePerformanceListByIds(List<Integer> ids) {
        // 删除
        studentCoursePerformanceMapper.deleteByIds(ids);
        }


    private void validateStudentCoursePerformanceExists(Integer id) {
        if (studentCoursePerformanceMapper.selectById(id) == null) {
            throw exception(STUDENT_COURSE_PERFORMANCE_NOT_EXISTS);
        }
    }

    @Override
    public StudentCoursePerformanceStudentDTO getStudentCoursePerformance(Integer id) {
        return studentCoursePerformanceMapper.selectById(id);
    }

    @Override
    public PageResult<StudentCoursePerformanceStudentDTO> getStudentCoursePerformancePage(StudentCoursePerformancePageReqVO pageReqVO) {
        return studentCoursePerformanceMapper.selectPage(pageReqVO);
    }

}
